import io

from fontTools.ttLib import TTFont
from PIL import Image,ImageDraw,ImageFont
from ddddocr import DdddOcr


font = TTFont('./static/fonts/file')
ocr = DdddOcr(show_ad=False)
font_maps={}


# 2.指定绘制的字体文件
img_font = ImageFont.truetype('./static/fonts/file',size=25)
for code,name in font.getBestCmap().items():
    if code == 120:
        continue
    else:
        # 将字符 使用特定的字体文件 渲染到图上
        print(code,chr(code),name)
        # 1.创建32*32的白色RGB图
        img = Image.new('RGBA',(32,32),'white')

        # 3.绘制图形到图片
        draw = ImageDraw.Draw(img)
        draw.text((0,0),chr(code),font=img_font)

        # 4.保存图片到本地 或者 字节流
        # img.save('./static/fonts/img/'+code+'.png')

        img_bytes = io.BytesIO()
        img.save(img_bytes,format='PNG')

        r = ocr.classification(img_bytes.getvalue())
        print(code,'识别结果',r)
        font_maps[chr(code)] = r

print(font_maps)
